<?php

namespace app\admin\controller;

use think\facade\Request;
use app\common\Error;
use app\common\Common;
use app\admin\service\ResponsibilityPersonService;

class ResponsibilityPerson extends Base
{
    private $service;

    public function __construct(ResponsibilityPersonService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    // 渲染列表页
    public function index()
    {
        return view();
    }

    // 渲染详情页
    public function info()
    {
        return view();
    }

    // 添加负责人
    public function add()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\ResponsibilityPerson::class)->scene('add')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editResponsibilityPerson($params);

        if (!$result || !empty($result['message'])) {
            $message = !empty($result['message']) ? $result['message'] : Error::FAIL_MESSAGE;
            return Common::fail($message);
        }

        return Common::success();
    }

    // 编辑负责人
    public function edit()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\ResponsibilityPerson::class)->scene('edit')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editResponsibilityPerson($params);

        if (!$result) {
            return Common::fail(Error::FAIL_MESSAGE);
        }

        return Common::success();
    }

    // 负责人列表
    public function list()
    {
        $params = Request::param();

        $data = $this->service->getResponsibilityPersonList($params);

        return Common::success($data);
    }

    // 负责人详情
    public function detail()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\ResponsibilityPerson::class)->scene('detail')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getResponsibilityPersonDetail($params['id']);

        return Common::success($data);
    }

    // 删除负责人
    public function delete()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\ResponsibilityPerson::class)->scene('delete')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->deleteResponsibilityPerson($params['id']);

        if (!$result) {
            return Common::fail();
        }
        return Common::success();
    }

    // 下拉列表
    public function select()
    {
        $params = Request::param();
        $data = $this->service->getSelectList($params);
        return Common::success($data);
    }

    // 下拉列表
    public function distributorSelect()
    {
        $params = Request::param();
        $data = $this->service->getDistributorSelect($params);
        return Common::success($data);
    }
}